
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
import os


html_header = "../html/html_header.txt"

'''
    generic day object to get the events for a particular day
    from the DB
    need to pass day as a parameter somehow
     - possibly by POSTing some information when a user clicks on the
     image of a particular day. eg: user clicks "monday", monday is POSTED,
     we use that to render the events for monday.
'''

'''
        figure out how to get POST data in Python
        use POST data to fill these in
    '''
    
class htmlbuilder:
    
    
        def print_header(self):
            
            #header = open(html_header, 'r').read()
            header = """
            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

             "http://www.w3.org/TR/html4/loose.dtd"> 
        
            <html>

            <link rel="stylesheet" type="text/css" href="stylesheets/main.css" />
            <head>

            </head>"""
            return header
        
        def print_footer(self):
            
            footer = """</body>

            </html>"""
            return footer
        
        def print_body(self,day,containers):
            body = ""
            body_title = """<body>
            <p class = "side_title"><font size = "30">"""+ day +"""</font></p>
            """
            body+=body_title
            body+="""<div class = day_items>"""
            
            body+= self.print_multiple_containers(containers)
            
            body+="""</div>"""
            body += "</body>"
            return body
        
        def print_container(self,icons):
            container="""<div class = "container">"""
            
            container_icons = self.print_icons(icons)
            container+=container_icons
            container+="""</div>"""
            return container
        
        def print_multiple_containers(self,containers):
            containers_result=""
            for contain in containers:
                containers_result += self.print_container(contain)
            
            return containers_result
        
            
            
        def print_icons(self, icons):
            icon_container ="""<div class = "entry_details">"""
            
            for icon in icons:
                icon_list ="""<img class = "zoom" src="images/"""
                icon += ".png" +'"'
                icon +=""" alt="">"""
                icon_list+=icon
                icon_container+=icon_list
                
            icon_container+="""</div>"""
            return icon_container
        
        def generate(self,events, day):
            
            finalstring=""
            finalstring += self.print_header()
            finalstring += self.print_body(day,events)
            finalstring += self.print_footer()
            return finalstring;
        
        def create_club_icon(self, event, icon):
            url = event["eventlink"]
            profile_pic_link = event["profilepic"]
            icon = "<a href='" + url + "'>" 
            
            icon += """<img class = "zoom" src=""" + profile_pic_link
            icon +=""" alt="">"""
            icon += "</a>"
                
            return icon
        
        def put_into_club_icon(self, event,icon):
            result = ""
            
            return result;
        
        def create_location_icon(self,event,icon):
            url = self.put_into_gmaps_url(event["address"])
            icon = "<a href='" + url + "'>" 
            
            icon += """<img class = "zoom" src="images/"""
            icon += "Location" +""".png"""

            icon +=""" alt="">"""
            icon += "</a>"
                
            return icon
                
        def put_into_gmaps_url(self,addr):
            encoded = ""
            
            for c in addr:
                
                if c == ' ':
                    c = '+'
                    
                encoded += c
            url = "https://maps.google.ie/maps?q=" + encoded
            return url
            


class DayPage(webapp.RequestHandler):
    
    
    '''
     Post function for generating page for a specific day
    '''
    def post(self):
        
       
        post_data = self.request.get('day') #gets the day
        event1 = {"ClubLogo","Age18","EntryFifteen","Location","Promoted","VIP"}
        
        events = []
        events.append(event1)
        
        builder = htmlbuilder()
        htmlpage = builder.generate(events, post_data)
        self.response.out.write(htmlpage)
        
            

application = webapp.WSGIApplication([('/event_day', DayPage)], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()